﻿using System;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace WebMsg
{
    public class SmsGlobal : WebMsg
    {
        private smsglobalws.MobileWorks _ws;
        private String _ticket;

        public SmsGlobal()
            : base()
        {
            this.messageURL = @"https://www.smsglobal.com.au/http-api.php?action=sendsms";
            this.balanceURL = @"https://www.smsglobal.com.au/http-api.php?action=balancesms";
            this.CustomFrom = true;

            _ws = new smsglobalws.MobileWorks();
            base.CredentialsUpdated += new CredentialsUpdatedHandler(SmsGlobal_CredentialsUpdated);
        }

        protected override void AddResults()
        {
            Results.Add("0", ResultCodes.Success);
            Results.Add("400", ResultCodes.ErrorTimeout);
            Results.Add("401", ResultCodes.ErrorDisabled);
            Results.Add("402", ResultCodes.ErrorNoResponse);
            Results.Add("8", ResultCodes.ErrorSystem);
            Results.Add("1", ResultCodes.ErrorMessageLength);
            Results.Add("14", ResultCodes.ErrorPassword);
        }

        void SmsGlobal_CredentialsUpdated(object sender, EventArgs args)
        {
            String xmlResponse = _ws.apiValidateLogin(User, Pass);
            using (StringReader reader = new StringReader(xmlResponse))
            {
                XDocument doc = XDocument.Load(reader);
                _ticket = doc.Root.Element("ticket").Value;
            }
        }

        protected override ResultCodes ParseResult(String rawResult)
        {
            return base.ParseResult(rawResult);            
        }

        public override String GetCreditsRemaining()
        {
            if (!HasCredentials)
                throw new ArgumentException("Username and Password have not been set.");

            String xmlResponse = _ws.apiBalanceSms(_ticket);

            String result;

            using (StringReader reader = new StringReader(xmlResponse))
            {
                XDocument doc = XDocument.Load(reader);
                result = doc.Root.Element("balance").Value;
            }

            if (String.IsNullOrWhiteSpace(result))
                return "Unknown.";

            if (Decimal.Parse(result) < 0)
                return "0";

            return result;
        }

        protected override String SendMessage(Sms message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            if (!HasCredentials)
                throw new ArgumentException("Username and Password have not been set.");

            String result;

            if (message.LongMessage == 1)
            {
                result = _ws.apiSendLongSms(_ticket, message.From.Number, message.To.Number, message.Body, "text", "0", "");
            }
            else
            {
                result = _ws.apiSendSms(_ticket, message.From.Number, message.To.Number, message.Body, "text", "0", "");
            }

            return result;
        }


    }
}
